package com.maizhi.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.maizhi.httpmodel.*;
import com.alibaba.fastjson.JSON;
import com.maizhi.model.*;
import com.maizhi.service.BaseServiceI;
import com.maizhi.service.UserServiceI;
import com.maizhi.util.*;
import com.sun.org.apache.commons.beanutils.BeanUtils;


@Controller
@RequestMapping("/loginController")
public class loginController extends BaseController {

	private Logger log = Logger.getLogger(loginController.class);
	
	//BaseServiceI<User> userService;
	BaseServiceI<MenuModel> menuService;
	UserServiceI    userService;

	public UserServiceI getUserService() {
		return userService;
	}

	@Autowired
	public void setUserService(UserServiceI userService) {
		this.userService = userService;
	}

	public BaseServiceI<MenuModel> getMenuService() {
		return menuService;
	}

	@Autowired
	public void setMenuService(BaseServiceI baseService) {
		this.menuService = baseService;
	}
	
	public UserI getLoginUser(HttpSession session){
		SessionInfo sessionInfo;
		try{
			sessionInfo = (SessionInfo)session.getAttribute(ResourceUtil.getSessionInfoName());
			if(sessionInfo==null){
				return null;
			}
		}catch(Exception e){
			return null;
		}
		return sessionInfo.getUser();
	}
	
	@RequestMapping(params = "login")
	@ResponseBody
	public Json login(UserI user, HttpSession session, HttpServletRequest request) {
		Json j = new Json();
		UserI u = userService.login(user);
		if (u != null) {
			j.setSuccess(true);
			j.setMsg("登录成功!");
			u.setIp(IpUtil.getIpAddr(request));
			SessionInfo sessionInfo = new SessionInfo();
			sessionInfo.setUser(u);
			session.setAttribute(ResourceUtil.getSessionInfoName(), sessionInfo);
			j.setObj(u);
		} else {
			j.setMsg("用户名或密码错误!");
		}
		return j;
	}
	
	@RequestMapping(params = "loginCheck")
	public ModelAndView loginCheck(HttpSession session, HttpServletRequest request) {
		ModelAndView model;
		UserI user = getLoginUser(session); 
		if(user!=null){
			model = new ModelAndView("/index");
			model.addObject("loginUser",user);
		}else{
			model = new ModelAndView("/login");
			model.addObject("loginMsg","会话超时或登录失败");
		}
		return model;
	}
	
	@RequestMapping(params = "loginOut")
	public ModelAndView loginOut(HttpSession session) {
		session.removeAttribute(ResourceUtil.getSessionInfoName());
		session.invalidate();
		ModelAndView model = new ModelAndView("/login");
		return model;
	}
}

























